package com.yoomiito.app.widget.wheelview;

import android.app.AlertDialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yoomiito.app.R;


public class DateSelectDialog extends AlertDialog implements OnWheelChangedListener {
    private static final int CURRENT_YEAR = Utils.getCurrentYear();
    private static final int CURRENT_MONTH = Utils.getCurrentMonth();
    private static final int CURRENT_DAY = Utils.getCurrentDay();

    private int mWidth = ViewGroup.LayoutParams.MATCH_PARENT;
    private int mHeight = ViewGroup.LayoutParams.WRAP_CONTENT;
    private int startYear = 1970;
    private int endYear = CURRENT_YEAR;
    private int yearIndex;


    private int monthIndex;
    private int dayIndex;
    private Context mContext;

    private String[] years;
    private String[] months;
    private String[] days;
    private WheelView wvYear;
    private WheelView wvMonth;
    private WheelView wvDay;
    private int defaultSelectYear = CURRENT_YEAR;
    private int defaultSelectMonth = CURRENT_MONTH;
    private int defaultSelectDay = CURRENT_DAY;
    private TextView cancelTv;
    private TextView sureTv;
    private TextView tvDay;

    public boolean isShow = true;
    private OnDateWheelViewListener listener;
    public DateSelectDialog(@NonNull Context context) {
        this(context,0);
    }

    public DateSelectDialog(@NonNull Context context, int theme) {
        super(context, theme);
        this.mContext = context;
    }

    private void initView() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_date,null);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(mWidth, mHeight);
        setView(view);

        cancelTv = view.findViewById(R.id.item_date_cancel);
        sureTv = view.findViewById(R.id.item_date_sure);
        tvDay = view.findViewById(R.id.tv_day);
        cancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DateSelectDialog.this.dismiss();
            }
        });
        sureTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener!=null){
                    DateSelectDialog.this.dismiss();
                    if(isShow){
                        listener.onResult(defaultSelectYear+"-"+add0(defaultSelectMonth)+"-"+add0(defaultSelectDay));
                    }else{
                        listener.onResult(defaultSelectYear+"-"+add0(defaultSelectMonth));
                    }
                }
            }
        });

        wvYear = view.findViewById(R.id.item_wv_1);
        wvMonth = view.findViewById(R.id.item_wv_2);
        wvDay = view.findViewById(R.id.item_wv_3);

        if(!isShow){
            tvDay.setVisibility(View.GONE);
            wvDay.setVisibility(View.GONE);
        }

        setWheelView(wvYear,years,yearIndex);
        setWheelView(wvMonth,months,monthIndex);
        setWheelView(wvDay,days,dayIndex);

    }

    private void setWheelView(WheelView wheelView,String[] entries,int currentIndex){
        wheelView.setEntries(entries);
        wheelView.setVisibleItems(4);
        wheelView.setCyclic(true);
        wheelView.setCurrentIndex(currentIndex);
        wheelView.setOnWheelChangedListener(this);
    }

    @Override
    public void onChanged(WheelView view, int oldIndex, int newIndex) {
        switch (view.getId()){
            case R.id.item_wv_1:
                defaultSelectYear = Integer.parseInt(view.getItem(newIndex).toString());
                wvMonth.setCurrentIndex(0);
                break;
            case R.id.item_wv_2:
                defaultSelectMonth = Integer.parseInt(view.getItem(newIndex).toString());
                updateDays();
                break;
            case R.id.item_wv_3:
                defaultSelectDay = Integer.parseInt(view.getItem(newIndex).toString());
                break;
        }

    }


    public void show(){

        months = Utils.months;
        days = Utils.getDaysOfMonth(defaultSelectYear,defaultSelectMonth);
        years = Utils.getYears(startYear,endYear);

        yearIndex = Utils.getArrayIndex(years,defaultSelectYear+"");
        monthIndex = Utils.getArrayIndex(months,defaultSelectMonth+"");
        dayIndex = Utils.getArrayIndex(days,defaultSelectDay+"");

        initView();
        super.show();

    }

    private void updateDays(){
        days = Utils.getDaysOfMonth(defaultSelectYear,defaultSelectMonth);
        wvDay.setEntries(days);
    }

    public DateSelectDialog gone(){

        isShow = false;
        return this;
    }


    private String add0(int i){
        if (i>=0&&i<10){
            return "0"+i;
        }
        return String.valueOf(i);
    }

    /**
     * 设置dialog的宽
     * @param mWidth
     * @return
     */
    public DateSelectDialog setmWidth(int mWidth) {
        this.mWidth = Utils.dip2px(mContext,mWidth);
        return this;
    }

    /**
     * 设置dialog的高
     * @param mHeight
     * @return
     */
    public DateSelectDialog setmHeight(int mHeight) {
        this.mHeight = Utils.dip2px(mContext,mHeight);
        return this;
    }

    /**
     * 设置开始年份
     * @param startYear
     * @return
     */
    public DateSelectDialog setStartYear(int startYear) {
        this.startYear = startYear;
        return this;
    }



    /**
     * 设置结束年份
     * @param endYear
     * @return
     */
    public DateSelectDialog setEndYear(int endYear) {
        this.endYear = endYear;
        return this;
    }

    public DateSelectDialog setDefaultSelectDate(int year, int month, int day){
        this.defaultSelectYear = year;
        this.defaultSelectMonth = month;
        this.defaultSelectDay = day;
        return this;
    }

    public DateSelectDialog setListener(OnDateWheelViewListener listener) {
        this.listener = listener;
        return this;
    }

    public interface OnDateWheelViewListener{
        void onResult(String date);
    }
}
